package com.olap.starter.common.util;

import com.alibaba.druid.pool.DruidDataSource;
import com.baomidou.dynamic.datasource.DynamicRoutingDataSource;
import com.baomidou.dynamic.datasource.ds.ItemDataSource;
import com.baomidou.mybatisplus.annotation.DbType;
import com.zaxxer.hikari.HikariDataSource;
import org.springframework.jdbc.core.JdbcTemplate;

import javax.sql.DataSource;


public class DBType {

    public static String getType(JdbcTemplate jdbcTemplate) {
        if (jdbcTemplate.getDataSource() instanceof HikariDataSource) {
            return getType(((HikariDataSource) jdbcTemplate.getDataSource()).getJdbcUrl()).getDb();
        } else if (jdbcTemplate.getDataSource() instanceof DynamicRoutingDataSource) {
            DynamicRoutingDataSource dynamicDataSource = (DynamicRoutingDataSource) jdbcTemplate.getDataSource();
            DataSource ds = null;
            try {
                ds = dynamicDataSource.getDataSource("online");
            } catch (Exception e) {
                // 忽略异常，继续尝试获取第一个HikariDataSource
            }
            if (ds == null) {
                for (DataSource dataSource : dynamicDataSource.getDataSources().values()) {
                    if (dataSource instanceof HikariDataSource) {
                        ds = dataSource;
                        break;
                    }
                }
            }
            if (ds instanceof HikariDataSource) {
                return getType(((HikariDataSource) ds).getJdbcUrl()).getDb();
            }else if (ds instanceof ItemDataSource) {
                ItemDataSource dataSource = (ItemDataSource) ds;
                if (dataSource.getDataSource() instanceof HikariDataSource) {
                    return getType(((HikariDataSource) dataSource.getDataSource()).getJdbcUrl()).getDb();
                }
                if (dataSource.getDataSource() instanceof DruidDataSource) {
                    return getType(((DruidDataSource) dataSource.getDataSource()).getUrl()).getDb();
                }
            }
        } else if (jdbcTemplate.getDataSource() instanceof ItemDataSource) {
            ItemDataSource dataSource = (ItemDataSource) jdbcTemplate.getDataSource();
            if (dataSource.getDataSource() instanceof HikariDataSource) {
                return getType(((HikariDataSource) dataSource.getDataSource()).getJdbcUrl()).getDb();
            }
            if (dataSource.getDataSource() instanceof DruidDataSource) {
                return getType(((DruidDataSource) dataSource.getDataSource()).getUrl()).getDb();
            }
        }
        throw new RuntimeException("当前不支持自定义数据源，请配置数据源的数据库连接！");
    }

    public static DbType getType(String str) {
        if (!str.contains(":mysql:") && !str.contains(":cobar:")) {
            if (str.contains(":oracle:")) {
                return DbType.ORACLE;
            } else if (str.contains(":postgresql:")) {
                return DbType.POSTGRE_SQL;
            } else if (str.contains(":sqlserver:")) {
                return DbType.SQL_SERVER;
            } else if (str.contains(":db2:")) {
                return DbType.DB2;
            } else if (str.contains(":mariadb:")) {
                return DbType.MARIADB;
            } else if (str.contains(":sqlite:")) {
                return DbType.SQLITE;
            } else if (str.contains(":h2:")) {
                return DbType.H2;
            } else if (!str.contains(":kingbase:") && !str.contains(":kingbase8:")) {
                if (str.contains(":dm:")) {
                    return DbType.DM;
                } else if (str.contains(":zenith:")) {
                    return DbType.GAUSS;
                } else if (str.contains(":oscar:")) {
                    return DbType.OSCAR;
                } else if (str.contains(":firebird:")) {
                    return DbType.FIREBIRD;
                } else {
                    return str.contains(":xugu:") ? DbType.XU_GU : DbType.OTHER;
                }
            } else {
                return DbType.KINGBASE_ES;
            }
        } else {
            return DbType.MYSQL;
        }
    }
}
